﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;

namespace JAAF.Builders.Configuration
{
    /// <summary>
    /// Contains information on an argument passed to a constructor or method call
    /// </summary
    public class Argument : ConfigurationElement
    {
        #region Configuration property declarations
        
        private static ConfigurationProperty s_oNameProp = new ConfigurationProperty("Name", typeof(string));

        private static ConfigurationProperty s_oTypeProp = new ConfigurationProperty("Type", typeof(string));
        
        private static ConfigurationProperty s_oValueProp = new ConfigurationProperty("Value", typeof(object));

        #endregion


        private Type m_oValueType;

        private object m_oValue;

        [ConfigurationProperty("Name", IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)this[s_oNameProp];
            }
        }

        [ConfigurationProperty("Type", IsRequired = true)]
        public string TypeName
        {
            get
            {
                return (string)base[s_oTypeProp];
            }
        }

        public Type Type
        {
            get
            {
                if (m_oValueType == null)
                {
                    m_oValueType = Type.GetType(this.TypeName);
                }

                return m_oValueType;
            }
        }


        [ConfigurationProperty("Value", IsRequired = true)]
        public string StringValue
        {
            get
            {
                return (string)base[s_oValueProp];
            }
        }

        public object Value
        {
            get
            {
                if (m_oValue == null)
                {
                    m_oValue = Convert.ChangeType(this.StringValue, this.Type);
                }

                return m_oValue;
            }
        }
    }
}
